/**
 * Qiangungun.com Inc.
 * Copyright (c) 2015-2015 All Rights Reserved.
 */
package com.qiangungun.report.biz.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.qiangungun.core.enums.BizCode;
import com.qiangungun.core.framework.pigeon.template.Action;
import com.qiangungun.core.framework.pigeon.template.Process;
import com.qiangungun.core.framework.pigeon.template.Template;
import com.qiangungun.core.framework.pigeon.template.TemplateManager;
import com.qiangungun.report.biz.share.ActionContext;
import com.qiangungun.report.biz.share.BaseAction;
import com.qiangungun.report.common.model.constants.Constants;
import com.qiangungun.report.common.model.exceptions.RptServiceException;
import com.qiangungun.report.service.facade.ReportNotifyService;
import com.qiangungun.report.service.model.ServiceRequest;
import com.qiangungun.report.service.model.ServiceResult;
import com.qiangungun.report.service.model.build.ReportBuildResult;
import com.qiangungun.report.service.model.config.RptcfgDefine;
import com.qiangungun.report.service.model.config.RptcfgQueryParam;

/**
 * 
 * @author hangdahong
 * @version $Id: ReportNotifyServiceImpl.java, v 0.1 2015年3月3日 下午7:31:05 hangdahong Exp $
 */
public class ReportNotifyServiceImpl implements ReportNotifyService{
    private static final Logger logger=LoggerFactory.getLogger(ReportNotifyServiceImpl.class);
    
    private TemplateManager reportTemplateManager;

    /** 
     * @see com.qiangungun.report.service.facade.ReportNotifyService#sendNotify(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public ServiceResult<ReportBuildResult> sendNotify( ServiceRequest<String> params) {
        logger.info("接收请求参数：request="+params);
        ServiceResult<ReportBuildResult> resp=new ServiceResult<ReportBuildResult>();
        ActionContext context=ActionContext.getContext();
        try{
            Template template = reportTemplateManager.getTemplateByName(Constants.TPL_BUILD_NAME);
            Assert.notNull(template);
            Process process=template.getProcessMap().get(Constants.PROC_REPORT_NAME);
            Assert.notNull(process);
            context.setRequest(params);
            context.setResponse(resp);
            for (Action action : process.getActionList()) {
                BaseAction baseAction = (BaseAction) action.getBean();
                baseAction.perform();
                if(context.isEnd()){
                    break;
                }
            }
        }catch(RptServiceException re){
            logger.error("请求处理异常",re);
            resp.setCode(re.getBizCode());
        }catch(Exception e){
            logger.error("请求处理异常",e);
            resp.setCode(BizCode.Unknown);
        }finally{
            ActionContext.removeContext();
        }
        
        return resp;
    }
    
    
    /** 
     * @see com.qiangungun.report.service.facade.ReportNotifyService#queryCfgDefine(java.lang.String, java.lang.String, com.qiangungun.report.service.model.ServiceRequest)
     */
    @Override
    public ServiceResult<RptcfgDefine> queryCfgDefine(ServiceRequest<Long> param) {
        logger.info("接收请求参数：request="+param);
        ServiceResult<RptcfgDefine> resp=new ServiceResult<RptcfgDefine>();
        ActionContext context=ActionContext.getContext();
        try{
            Template template = reportTemplateManager.getTemplateByName(Constants.TPL_QUERY_NAME);
            Assert.notNull(template);
            Process process=template.getProcessMap().get(Constants.PROC_DEFINE_NAME);
            Assert.notNull(process);
            context.setRequest(param);
            context.setResponse(resp);
            for (Action action : process.getActionList()) {
                BaseAction baseAction = (BaseAction) action.getBean();
                baseAction.perform();
                if(context.isEnd()){
                    break;
                }
            }
        }catch(RptServiceException re){
            logger.error("请求处理异常",re);
            resp.setCode(re.getBizCode());
        }catch(Exception e){
            logger.error("请求处理异常",e);
            resp.setCode(BizCode.Unknown);
        }finally{
            ActionContext.removeContext();
        }
        
        return resp;
    }




    /** 
     * @see com.qiangungun.report.service.facade.ReportNotifyService#queryCfgQryParamByDefineId(java.lang.String, java.lang.String, com.qiangungun.report.service.model.ServiceRequest)
     */
    @Override
    public ServiceResult<List<RptcfgQueryParam>> queryCfgQryParamByDefineId(ServiceRequest<Long> param) {
        logger.info("接收请求参数：request="+param);
        ServiceResult<List<RptcfgQueryParam>> resp=new ServiceResult<List<RptcfgQueryParam>>();
        ActionContext context=ActionContext.getContext();
        try{
            Template template = reportTemplateManager.getTemplateByName(Constants.TPL_QUERY_NAME);
            Assert.notNull(template);
            Process process=template.getProcessMap().get(Constants.PROC_QUERY_PARAM_NAME);
            Assert.notNull(process);
            context.setRequest(param);
            context.setResponse(resp);
            for (Action action : process.getActionList()) {
                BaseAction baseAction = (BaseAction) action.getBean();
                baseAction.perform();
                if(context.isEnd()){
                    break;
                }
            }
        }catch(RptServiceException re){
            logger.error("请求处理异常",re);
            resp.setCode(re.getBizCode());
        }catch(Exception e){
            logger.error("请求处理异常",e);
            resp.setCode(BizCode.Unknown);
        }finally{
            ActionContext.removeContext();
        }
        
        return resp;
    }

    /**
     * Setter method for property <tt>reportTemplateManager</tt>.
     * 
     * @param reportTemplateManager value to be assigned to property reportTemplateManager
     */
    public void setReportTemplateManager(TemplateManager reportTemplateManager) {
        this.reportTemplateManager = reportTemplateManager;
    }




    
    
    

}
